package com.bdzl.auth.controller;

import com.bdzl.auth.common.utils.JwtUtil;
import com.bdzl.auth.common.utils.MD5Utils;
import com.bdzl.auth.common.utils.oConvertUtils;
import com.bdzl.auth.service.UserRoleService;
import com.bdzl.auth.service.UserService;
import com.bdzl.pub.controller.BaseController;
import com.bdzl.pub.entity.sys.UserDO;
import com.bdzl.pub.util.param.PageUtils;
import com.bdzl.pub.util.param.Query;
import com.bdzl.pub.util.param.R;
import com.bdzl.pub.vo.sys.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 系统用户/客户账号表
 *
 * @author yangyicai
 * @email yangyicai@foxmail.com
 * @date 2021-03-23 13:59:22
 */

@Controller
@RequestMapping("/bdzl/user")
public class UserController extends BaseController {
	@Autowired
	private UserService userService;
	@Autowired
	private UserRoleService userRoleService;

	@GetMapping()
	String User(){
	    return "bdzl/user/user";
	}

	@ResponseBody
	@GetMapping("/list")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<UserDO> userList = userService.list(query);
		int total = userService.count(query);
		PageUtils pageUtils = new PageUtils(userList, total);
		return pageUtils;
	}

	@GetMapping("/add")
	String add(){
	    return "bdzl/user/add";
	}

	/**
	 * @Author Zhn
	 * @Description //TODO 修改密码
	 * @Date 16:42 2021/5/12
	 * @Param [userId, password, oldPassword]
	 * @return com.bdzl.pub.util.param.R
	 **/
	@PostMapping("/changePassword")
	@ResponseBody
	R changePassword(Integer userId,String password,String oldPassword){
		UserDO userDO = userService.get(userId);
		oldPassword = MD5Utils.encrypt(userDO.getUserLgname(), oldPassword);
		if (userDO.getUserPwd().equals(oldPassword)){
			password = MD5Utils.encrypt(userDO.getUserLgname(), password);
			userDO.setUserPwd(password);
			if (userService.update(userDO)>0){
				return R.ok(userDO);
			}
			return R.error("修改失败!");
		}
		return R.error("密码错误请重新输入!");
	}

	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@Transactional
	public R save(@RequestBody UserVO user) {
		return userService.saveUserDO(user);
	}
	/**
	 * @Author Zhn
	 * @Description //TODO 修改账户信息接口
	 * @Date 17:09 2021/5/12
	 * @Param [user] 账户
	 * @return com.bdzl.pub.util.param.R
	 **/
	@ResponseBody
	@RequestMapping("/update")
	@Transactional
	public R update(@RequestBody UserVO user,HttpServletRequest request){
		String accessToken = request.getHeader("X-Access-Token");
		String username = JwtUtil.getUsername(accessToken);
		UserDO byUserLgname = userService.getByUserLgname(username);
		if (user.getUserStatus().equals("1")) {
			if (byUserLgname.getUserId().equals(user.getUserId())) {
				R.error("不能禁用当前使用账号！");
			}
			if (user.getUserId().equals("1")) {
				R.error("管理员账号不能禁用！");
			}
		}
		return userService.updateUserDO(user);
	}

	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	public R remove(@RequestBody UserVO user, HttpServletRequest request){
		String accessToken = request.getHeader("X-Access-Token");
		String username = JwtUtil.getUsername(accessToken);
		UserDO byUserLgname = userService.getByUserLgname(username);
		if (byUserLgname.getUserId().equals(user.getUserId())){
			R.error("不能删除当前使用账号！");
		}
		if(user.getUserId() == 1){
			return R.error("系统理员不能删除！");
		}
		if(userService.remove(user.getUserId())>0){
		return R.ok();
		}
		return R.error();
	}


	/**
	 * @Author Zhn
	 * @Description //TODO 解除openid绑定
	 * @Date 17:09 2021/5/12
	 * @Param [userId] 用户ID
	 * @return com.bdzl.pub.util.param.R
	 **/
	@PostMapping("/unbundling")
	@ResponseBody
	public R unbundling(Integer userId){
		UserDO userDO = userService.get(userId);
		userDO.setUserOpenid("");
		if (userService.update(userDO)>0){
			return R.ok("解绑成功");
		}
		return R.error("解绑失败");
	}

	/**
	* @Description: TODO 根据userid查询用户信息
	* @param userId
	* @return: com.bdzl.pub.util.param.R
	* @Author: Ming
	* @Date: 2021/5/13 16:49
	*/
	@PostMapping("/getUserById")
	@ResponseBody
	public UserDO getUserById(@RequestParam("userId") Integer userId){
		return userService.get(userId);
	}

	/**
	 * @Author Zhn
	 * @Description //TODO 获取当前登录用户
	 * @Date 18:42 2021/5/19
	 * @Param [request]
	 * @return com.bdzl.pub.entity.sys.UserDO
	 **/
	@PostMapping("/getUser")
	@ResponseBody
	public UserDO getUserByToken(@RequestParam("token")String token) {
		String username = JwtUtil.getUsername(token);
		if (oConvertUtils.isEmpty(username)) {
			return null;
		}
		UserDO byUserLgname = userService.getByUserLgname(username);
		return byUserLgname;
	}

	/**
	 * @Author Zhn
	 * @Description //TODO 获取当前登录用户ID
	 * @Date 18:42 2021/5/19
	 * @Param [request]
	 * @return java.lang.Integer
	 **/
	@PostMapping("/getUserId")
	@ResponseBody
	public Integer getUserIdByToken(@RequestParam("token")String token) {
		String username = JwtUtil.getUsername(token);
		if (oConvertUtils.isEmpty(username)) {
			return null;
		}
		UserDO byUserLgname = userService.getByUserLgname(username);
		return byUserLgname.getUserId();
	}

	/**
	 * @Description: TODO 根据userid查询用户信息
	 * @param userId
	 * @return: com.bdzl.pub.util.param.R
	 * @Author: Ming
	 * @Date: 2021/5/13 16:49
	 */
	@GetMapping("/edit")
	@ResponseBody
	public UserDO getUser(Integer userId){
		UserDO userDO = userService.get(userId);
		UserVO userVO = new UserVO();
		//类拷贝
		BeanUtils.copyProperties(userDO, userVO);
		List<Integer> list = userRoleService.listRoleId(userId);
		userVO.setRoleList(list);
		return userVO;
	}

	/**
	* @Description: TODO 获取所有家访员
	* @return: java.util.List<com.bdzl.pub.entity.sys.UserDO>
	* @Author: Ming
	* @Date: 2021/5/25 18:03
	*/
	@ResponseBody
	@GetMapping("/listAll")
	public List<UserDO> listAll(){
		//查询列表数据
		return userService.getHomeVisit();
	}

}
